var isLogin = function() {
    if (isNaN($.cookie("work_username"))) {
        $("a.logout").hide();
    }
}

isLogin();

var LOG_SERVER_HOST = "http://132.232.16.135/log";

function rm_from_cookie() {
    $.removeCookie("index", {path:"/"});
    $.removeCookie("work_username", {path:"/"});
}

function combine(action, job_code) {
    let user_id = $.cookie("work_username");
    let act_time = new Date().getTime();
    let act = action;
    let job = job_code;
    let result = {
        "user_id": user_id,
        "act_time": act_time,
        "action": action
    };
    if (action != 'login' && action != 'logout') {
        result["job_code"] = job_code;
    }
    if (action === 'collect' || action === 'click') {
        result["good_id"] = $.cookie("good_id")
    }
    return result;
}

function sendLog(job_code, action) {
    $.ajax({
        url: LOG_SERVER_HOST,
        type: "POST",
        async: false,
        data: combine(action, job_code),
        dataType: "json",
        success: function (data) {},
        error: function () {console.error("请求失败！！！")}
    })
}

// 点击
function click(id) {
    let job_code = "click_" + $.cookie("index");
    $.cookie("index", Number($.cookie("index")) + 1);
    $.cookie("good_id", String(id));
    sendLog(job_code, "click");
    window.location.href = "item.html?good_id="+id;
}

// 收藏
function collect() {
    let id = queryParam("good_id")
    $.cookie("good_id", String(id));
    let job_code = "collect_" + $.cookie("index");
    $.cookie("index", Number($.cookie("index")) + 1);
    sendLog(job_code, "collect");
}

function queryParam(q) {
    let s = location.href.replace("?", "?&").split("&")
    for (i=1; i < s.length; i ++) {
        if (s[i].indexOf(q+"=")==0) {
            return s[i].replace(q+"=", "")
        }
    }
    return "";
}

// 下单
function order() {
    let job_code = "order_" + $.cookie("index");
    $.cookie("index", Number($.cookie("index")) + 1);
    sendLog(job_code, "order");
    window.location.href = "pay.html";
}

// 支付
function pay() {
    let job_code = "pay_" + $.cookie("index");
    $.cookie("index", Number($.cookie("index")) + 1);
    sendLog(job_code, "pay");
    window.location.href = "list.html";
}

// 登陆
function login() {
    if (typeof($.cookie("work_username")) != "undefined"){
        rm_from_cookie();
    }
    if (isNaN($.cookie("index"))) $.cookie("index", 0);
    let job_code = "login_" + $.cookie("index");
    let username = $("input.username").val();
    if (username === "") {
        return ;
    }
    $.cookie("work_username", String(username), {path: "/"})
    $.cookie("index", Number($.cookie("index")) + 1);
    sendLog(job_code, "login");
    window.location.href = "list.html";
}

function logout() {
    if (typeof($.cookie("work_username")) === "undefined"){
        return ;
    }

    let job_code = "logout_" + $.cookie("index");
    $.cookie("index", Number($.cookie("index")) + 1);
    sendLog(job_code, "logout");
    rm_from_cookie();
    window.location.href = "index.html";
}
